package tomsung.cloud.agdc.security;

import javax.servlet.http.HttpServletRequest;

import tomsung.cloud.agdc.bean.User;
import tomsung.cloud.agdc.common.Const;

public class SecurityUtil {

	/**
	 * 判断用户是否登录
	 * 
	 * @param request
	 * @return
	 */
	public static final boolean isLogin(HttpServletRequest request) {
		Object object = request.getSession().getAttribute(Const.USER_SESSION_KEY);
		return object != null;
	}

	/**
	 * 用户登录，将用户写入SESSION
	 * 
	 * @param request
	 * @param user
	 */
	public static final void login(HttpServletRequest request, User user) {
		request.getSession().setAttribute(Const.USER_SESSION_KEY, user);
	}
	
	/**
	 * 用户登出，删除SESSION
	 * 
	 * @param request
	 */
	public static final void logout(HttpServletRequest request) {
		request.getSession().removeAttribute(Const.USER_SESSION_KEY);
	}

	public static final User getLoginUser(HttpServletRequest request)
	{
		return (User) request.getSession().getAttribute(Const.USER_SESSION_KEY);
	}
}
